package com.eascs.web.o2o.scheduler.service;

import java.math.BigDecimal;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.web.o2o.scheduler.dao.ServiceDao;
import com.eascs.web.o2o.scheduler.entity.CompensatoryEntity;
import com.eascs.web.o2o.scheduler.util.Constant;
import com.eascs.web.o2o.scheduler.util.DateUtil;

@Service
public class SynoInfoAssetStructureBjService {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Inject
	ServiceDao serviceDao;

	/**
	 * @Title: saveAssetStructurePrincipalDaily
	 * @Description: report-10175 资产结构本金日报 (数据入库操作)
	 * @author lwx
	 * @return void
	 * @throws @date
	 *            
	 */
	public void saveAssetStructurePrincipalDaily() {
		logger.info("[Service库]资产结构本金日报表数据同步开始.........");

		logger.info("【删除】产结构本金日报表数据开始-------------------------------------");
		String deleteSql = " DELETE FROM asset_structure_principal_daily ";
		serviceDao.excuteSql(deleteSql);
		logger.info("【删除】产结构本金日报表数据结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】贷款单号，贷款id，省区
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓----------------------------------------------------

		logger.info("【插入】贷款单号，贷款id，省区字段开始-------------------------------------");
		String insertSql = "INSERT INTO asset_structure_principal_daily ( id, bill_code, loanapply_no, province, create_date )   SELECT UUID(), o.bill_code, t.loanapply_no, opr.regional_name, DATE_FORMAT(now(), '%Y-%m-%d') from o2o_after_loan_for_billcode o left  join o2o_loanapply t on o.bill_code=t.bill_code and t.loan_flg='Y'  inner join o2o_bank_loan_data ab on t.bill_code =ab.bill_code  inner join o2o_platform_regional opr ON opr.platform_id = t.ltd where ab.loan_bank <> 'WEBANK' ";
		try {
			serviceDao.excuteSql(insertSql);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("错误:保存【贷款单号，贷款id，省区】数据库操作错误");
		}

		logger.info("【插入】贷款单号，贷款id，省区字段结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】全部贷款余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓------------------------------------------------------------

		

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】未到期余额正常还息余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】未到期余额正常还息余额开始-------------------------------------");
		//getNormalAlsoBalanceNdb();
		String sql2="update asset_structure_principal_daily p set  p.normal_also_balance_ndb = (select  IFNULL(t.outstanding_of_deposits,0)  from o2o_after_loan_for_billcode t where ADDDATE(t.loan_date,INTERVAL t.loan_term MONTH) >= now() and t.bill_code = p.bill_code ) ";
		try {
			serviceDao.excuteSql(sql2);
		} catch (Exception e) {
			logger.error("更新资产结构日报表，未到期余额正常还息余额异常[{}]", e.getMessage());
		}
		
		logger.info("【插入】未到期余额正常还息余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】未到期余额利息逾期余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】未到期余额利息逾期余额开始-------------------------------------");
		//getInterestDueBalanceNdb();
		String sql3=" update asset_structure_principal_daily p  set  p.interest_due_balance_ndb = (select  IFNULL(t.outstanding_of_deposits,0)  from o2o_after_loan_for_billcode t where t.wf_status ='YQ' and  ADDDATE(t.loan_date,INTERVAL t.loan_term MONTH) >= now() and t.bill_code in (select o.billcode from o2o_overdue_report o where  o.`status`='1')  and t.bill_code = p.bill_code ) ";
		try {
			serviceDao.excuteSql(sql3);
		} catch (Exception e) {
			logger.error("更新资产结构日报表，未到期余额利息逾期余额异常[{}]", e.getMessage());
		}
		logger.info("【插入】未到期余额利息逾期余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】未到期余额利息代偿余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】未到期余额利息代偿余额开始-------------------------------------");
		//getInterestDCBalanceNdb();
		logger.info("【插入】未到期余额利息代偿余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】未到期余额合计
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------------

		logger.info("【插入】未到期余额合计开始-------------------------------------");
		//getTotalAmountNdbByBillCode();
		String sql4="update asset_structure_principal_daily set total_amount_ndb = ifnull(normal_also_balance_ndb,0)+ifnull(interest_due_balance_ndb,0) ";
		try {
			serviceDao.excuteSql(sql4);
		} catch (Exception e) {
			logger.error("更新资产结构日报表，未到期余额合计异常[{}]", e.getMessage());
		}
		logger.info("【插入】未到期余额合计结束-------------------------------------");
		
		
		// -------------------------------------我是分割线--------------------------------------------
// 【插入】已到期未结清余额到期未代偿余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额到期未代偿余额开始-------------------------------------");
		//getExpireNotdcBalanceEub();
		String sql5="update asset_structure_principal_daily p set  p.expire_notdc_balance_eub =  (select  ifnull(o.defaultcapital,0)  from o2o_overdue_report o where o.`status` ='2' and  o.billcode =p.bill_code ) ";
		try {
			serviceDao.excuteSql(sql5);
		} catch (Exception e) {
			logger.error("更新资产结构日报表，已到期未结清余额到期未代偿余额异常[{}]", e.getMessage());
		}
		logger.info("【插入】已到期未结清余额到期未代偿余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额代偿余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额代偿余额开始-------------------------------------");
		//getDCBalanceEub();
		String sql6="update asset_structure_principal_daily p set  p.dc_balance_eub = (select  ifnull(t.current_compensatory_balance,0)  from o2o_after_loan_for_billcode t  where   ADDDATE(t.loan_date,INTERVAL t.loan_term MONTH) < now() and t.bill_code = p.bill_code ) ";
		try {
			serviceDao.excuteSql(sql6);
		} catch (Exception e) {
			logger.error("更新资产结构日报表，已到期未结清余额到期未代偿余额异常[{}]", e.getMessage());
		}
		logger.info("【插入】已到期未结清余额代偿余额结束-------------------------------------");
		
		
		// 查询资产结构本金日报表中的全部bill_code
		logger.info("【插入】全部贷款余额开始-------------------------------------");
		//getLoanBalanceByBillCode();
		String sql1=" update asset_structure_principal_daily  p set  p.loan_balance =  ifnull(p.dc_balance_eub,0)+ ifnull(p.total_amount_ndb,0)+ifnull(p.expire_notdc_balance_eub,0) ";
		try {
			serviceDao.excuteSql(sql1);
		} catch (Exception e) {
			logger.error("更新资产结构日报表，全部贷款余额异常[{}]", e.getMessage());
		}
		logger.info("【插入】全部贷款余额结束-------------------------------------");

		
		
		
		
		
		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额分期余额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------
	
		logger.info("【插入】已到期未结清余额分期余额开始-------------------------------------");
		//getFQBalanceEub();
		String sql7="update asset_structure_principal_daily p  set  p.installment_balance_eub = (select  ifnull(t.current_compensatory_balance,0)  from o2o_after_loan_for_billcode t  where   ADDDATE(t.loan_date,INTERVAL t.loan_term MONTH) < now() and t.bill_code  in (select loan_billcode from o2o_compensatory_back  where is_delete='N' and flow_status='CLO' ) and t.bill_code = p.bill_code  ) ";
		try {
			serviceDao.excuteSql(sql7);
		} catch (Exception e) {
			logger.error("更新资产结构日报表，已到期未结清余额分期余额异常[{}]", e.getMessage());
		}
		logger.info("【插入】已到期未结清余额分期余额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额本年代偿回款金额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额本年代偿回款金额开始-------------------------------------");
		//getYearDCReceivableEub();
		String sql8=" update asset_structure_principal_daily p  set  p.year_dc_receivable_eub = (select SUM(IFNULL(t.this_payment_principal, 0)) as thisPaymentPrincipal from o2o_badloan_record t where t.is_delete='N' and t.flow_status='CLO' and t.this_payment_date  BETWEEN ? AND ? AND  t.bill_code = p.bill_code  group by t.bill_code) ";
		try {
			serviceDao.excuteSql(sql8,DateUtil.getCurrYearFirst(), DateUtil.getCurrYearLast());
		} catch (Exception e) {
			logger.error("更新资产结构日报表，已到期未结清余额本年代偿回款金额异常[{}]", e.getMessage());
		}
		logger.info("【插入】已到期未结清余额本年代偿回款金额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额本年代偿金额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额本年代偿金额开始-------------------------------------");
		//getYearDCAmountEub();
		String sql9="update asset_structure_principal_daily p set  p.year_dc_amount_eub = (select sum( ifnull(t.pay_amount,0)) as amount from o2o_ea_loan_data t where t.is_del='N' and t.loan_type ='DC'  and  t.pay_date BETWEEN ? AND ?   AND    t.bill_code = p.bill_code group by  t.bill_code) ";
		try {
			serviceDao.excuteSql(sql9,DateUtil.getCurrYearFirst(), DateUtil.getCurrYearLast());
		} catch (Exception e) {
			logger.error("更新资产结构日报表，已到期未结清余额本年代偿金额异常[{}]", e.getMessage());
		}
		logger.info("【插入】已到期未结清余额本年代偿金额结束-------------------------------------");

		// -------------------------------------我是分割线--------------------------------------------
		// 【插入】已到期未结清余额历年累计代偿金额
		// ↓↓↓↓↓↓↓↓↓↓↓↓↓-----------------------------------------------------

		logger.info("【插入】已到期未结清余额历年累计代偿金额开始-------------------------------------");
		//getHistoryDCAmountEub();
		String sql10="update asset_structure_principal_daily p set  p.history_dc_amount_eub= (select sum( ifnull(t.pay_amount,0)) as amount from o2o_ea_loan_data t where t.is_del='N'  and t.loan_type ='DC'  and  t.bill_code = p.bill_code group by  t.bill_code) ";
		try {
			serviceDao.excuteSql(sql10);
		} catch (Exception e) {
			logger.error("更新资产结构日报表，已到期未结清余额历年累计代偿金额异常[{}]", e.getMessage());
		}
		logger.info("【插入】已到期未结清余额历年累计代偿金额结束-------------------------------------");

		logger.info("[Service库]资产结构本金日报表数据同步结束.........");
	}

	// 根据bill_code查询【全部贷款余额】
	public void getLoanBalanceByBillCode() {
		int count = 0;
		int index = 0;
		String sql = "SELECT * FROM asset_structure_principal_daily ";
		while (index >= 0) {
			SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					String billCode = objs[1] == null ? null : (String) objs[1];
					String overdueSql = "SELECT   IFNULL(t.outstanding_of_deposits,0)+IFNULL(m.defaultcapital, 0) + IFNULL(m.defaultinterest, 0) + IFNULL(m.penaltyinterest, 0)  FROM o2o_after_loan_for_billcode t left join o2o_overdue_report m on t.bill_code=m.billcode  where  t.bill_code = ?  ";
					List<Object[]> list = serviceDao.findParams(overdueSql,billCode);
					if(list!=null&&list.size()>0){
						Object[] o= list.get(0);
						BigDecimal loan_balance = (BigDecimal) o[0];
						serviceDao.excuteSql("update asset_structure_principal_daily  set  loan_balance=?  where bill_code =? ",loan_balance, billCode);
						count++;
					}
				} catch (Exception e) {
					logger.error("单据:[{}]更新资产结构日报表，全部贷款余额异常[{}]", objs[0].toString(), e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("更新更新资产结构日报表，全部贷款余额条数:[{}]", count);
	}

	// 根据bill_code查询【未到期余额合计】
	public void getTotalAmountNdbByBillCode() {
		String sql="update asset_structure_principal_daily set total_amount_ndb = ifnull(normal_also_balance_ndb,0)+ifnull(interest_due_balance_ndb,0)";
		serviceDao.excuteSql(sql);
	}

	// 根据bill_code查询【未到期余额正常还息余额】
	public void getNormalAlsoBalanceNdb() {
		logger.info("开始更新资产结构日报表，未到期余额正常还息余额");
		int count = 0;
		int index = 0;
		String sql = "SELECT * FROM asset_structure_principal_daily ";
		while (index >= 0) {
			SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					String billCode = objs[1] == null ? null : (String) objs[1];
					String overdueSql = " select t.loan_amount  from o2o_after_loan_for_billcode t where t.wf_status ='O' and ADDDATE(t.loan_date,INTERVAL t.loan_term MONTH) >= now() and t.bill_code = ? ";
					List<Object[]> list = serviceDao.findParams(overdueSql,billCode);
					if(list!=null&&list.size()>0){
						Object[] o= list.get(0);
						BigDecimal loan_amount = (BigDecimal) o[0];
						serviceDao.excuteSql("update asset_structure_principal_daily  set  normal_also_balance_ndb=?  where bill_code =? ",loan_amount, billCode);
						count++;
					}
				} catch (Exception e) {
					logger.error("单据:[{}]更新资产结构日报表，未到期余额正常还息余额异常[{}]", objs[0].toString(), e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("更新更新资产结构日报表，未到期余额正常还息余额条数:[{}]", count);
	}

	// 根据bill_code查询【未到期余额利息逾期余额】
	public void getInterestDueBalanceNdb() {
		logger.info("开始更新资产结构日报表，未到期余额利息逾期余额");
		int count = 0;
		int index = 0;
		String sql = "SELECT * FROM asset_structure_principal_daily ";
		while (index >= 0) {
			SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					String billCode = objs[1] == null ? null : (String) objs[1];
					String overdueSql = " select t.loan_amount  from o2o_after_loan_for_billcode t where t.wf_status ='YQ' and  ADDDATE(t.loan_date,INTERVAL t.loan_term MONTH) >= now() and t.bill_code in (select o.billcode from o2o_overdue_report o where  o.`status`='1')  and t.bill_code = ? ";
					List<Object[]> list = serviceDao.findParams(overdueSql,billCode);
					if(list!=null&&list.size()>0){
						Object[] o= list.get(0);
						BigDecimal loan_amount = (BigDecimal) o[0];
						serviceDao.excuteSql("update asset_structure_principal_daily  set  interest_due_balance_ndb=?  where bill_code =? ",loan_amount, billCode);
						count++;
					}
				} catch (Exception e) {
					logger.error("单据:[{}]更新资产结构日报表，未到期余额利息逾期余额异常[{}]", objs[0].toString(), e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("更新更新资产结构日报表，未到期余额利息逾期余额条数:[{}]", count);
	}

	// 根据bill_code查询【未到期余额利息代偿余额】
	public void getInterestDCBalanceNdb() {
		logger.info("开始更新资产结构日报表，未到期余额利息代偿余额");
		int count = 0;
		int index = 0;
		String sql = "SELECT * FROM asset_structure_principal_daily ";
		while (index >= 0) {
			SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					String billCode = objs[1] == null ? null : (String) objs[1];
					String overdueSql = "select t.loan_amount  from o2o_after_loan_for_billcode t where t.wf_status ='YQ' and  ADDDATE(t.loan_date,INTERVAL t.loan_term MONTH) >= now() and t.bill_code = ?   and t.bill_code not in (select o.billcode from o2o_overdue_report o where  o.`status`='1')  and t.bill_code in (select bill_code_ea from o2o_ea_loan_data where loan_type ='LXDC')";
					List<Object[]> list = serviceDao.findParams(overdueSql,billCode);
					if(list!=null&&list.size()>0){
						Object[] o= list.get(0);
						BigDecimal loan_amount = (BigDecimal) o[0];
						serviceDao.excuteSql("update asset_structure_principal_daily  set  interest_dc_balance_ndb=?  where bill_code =? ",loan_amount, billCode);
						count++;
					}
				} catch (Exception e) {
					logger.error("单据:[{}]更新资产结构日报表，未到期余额利息代偿余额异常[{}]", objs[0].toString(), e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("更新更新资产结构日报表，未到期余额利息代偿余额条数:[{}]", count);
	}

	// 根据bill_code查询【已到期未结清余额到期未代偿余额】
	public void getExpireNotdcBalanceEub() {
		logger.info("开始更新资产结构日报表，已到期未结清余额到期未代偿余额");
		int count = 0;
		int index = 0;
		String sql = "SELECT * FROM asset_structure_principal_daily ";
		while (index >= 0) {
			SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					String billCode = objs[1] == null ? null : (String) objs[1];
					String overdueSql = " select o.defaultcapital from o2o_overdue_report o where o.`status` ='2' and  o.billcode =? ";
					List<Object[]> list = serviceDao.findParams(overdueSql,billCode);
					if(list!=null&&list.size()>0){
						Object[] o= list.get(0);
						BigDecimal loan_amount = (BigDecimal) o[0];
						serviceDao.excuteSql("update asset_structure_principal_daily  set  expire_notdc_balance_eub=?  where bill_code =? ",loan_amount, billCode);
						count++;
					}
				} catch (Exception e) {
					logger.error("单据:[{}]更新资产结构日报表，已到期未结清余额到期未代偿余额异常[{}]", objs[0].toString(), e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("更新更新资产结构日报表，已到期未结清余额到期未代偿余额条数:[{}]", count);
	}

	// 根据bill_code查询【已到期未结清余额代偿余额】
	public void getDCBalanceEub() {
		logger.info("开始更新资产结构日报表，已到期未结清余额代偿余额");
		int count = 0;
		int index = 0;
		String sql = "SELECT * FROM asset_structure_principal_daily ";
		while (index >= 0) {
			SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					String billCode = objs[1] == null ? null : (String) objs[1];
					String overdueSql = " select t.current_compensatory_balance from o2o_after_loan_for_billcode t  where   ADDDATE(t.loan_date,INTERVAL t.loan_term MONTH) < now() and t.bill_code = ?  ";
					List<Object[]> list = serviceDao.findParams(overdueSql,billCode);
					if(list!=null&&list.size()>0){
						Object[] o= list.get(0);
						BigDecimal loan_amount = (BigDecimal) o[0];
						serviceDao.excuteSql("update asset_structure_principal_daily  set  dc_balance_eub=?  where bill_code =? ",loan_amount, billCode);
						count++;
					}
				} catch (Exception e) {
					logger.error("单据:[{}]更新资产结构日报表，已到期未结清余额代偿余额异常[{}]", objs[0].toString(), e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("更新更新资产结构日报表，已到期未结清余额代偿余额条数:[{}]", count);
	}
	
	// 根据bill_code查询【已到期未结清余额分期余额】
	public void getFQBalanceEub() {
		logger.info("开始更新资产结构日报表，已到期未结清余额分期余额");
		int count = 0;
		int index = 0;
		String sql = "SELECT * FROM asset_structure_principal_daily ";
		while (index >= 0) {
			SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					String billCode = objs[1] == null ? null : (String) objs[1];
					String overdueSql = " select t.current_compensatory_balance from o2o_after_loan_for_billcode t  where   ADDDATE(t.loan_date,INTERVAL t.loan_term MONTH) < now() and t.bill_code in (select bill_code from o2o_ea_loan_data  where is_del='N' and  id in (select main_id  from o2o_ea_loan_data_td where is_del='N' )) and t.bill_code = ?  ";
					List<Object[]> list = serviceDao.findParams(overdueSql,billCode);
					if(list!=null&&list.size()>0){
						Object[] o= list.get(0);
						BigDecimal loan_amount = (BigDecimal) o[0];
						serviceDao.excuteSql("update asset_structure_principal_daily  set  dc_balance_eub=?  where bill_code =? ",loan_amount, billCode);
						count++;
					}
				} catch (Exception e) {
					logger.error("单据:[{}]更新资产结构日报表，已到期未结清余额分期余额异常[{}]", objs[0].toString(), e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("更新更新资产结构日报表，已到期未结清余额分期余额条数:[{}]", count);
	}
	

	// 根据bill_code查询【已到期未结清余额本年代偿回款金额】
	public void getYearDCReceivableEub() {
		logger.info("开始更新资产结构日报表，已到期未结清余额本年代偿回款金额");
		int count = 0;
		int index = 0;
		String sql = "SELECT * FROM asset_structure_principal_daily ";
		while (index >= 0) {
			SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					String billCode = objs[1] == null ? null : (String) objs[1];
					String paymentAmountSql="select SUM(IFNULL(this_payment_principal, 0)) as thisPaymentPrincipal from o2o_badloan_record "
							+ " where is_delete='N' and flow_status='CLO' and this_payment_date  BETWEEN ? AND ? AND  bill_code = ? group by bill_code ";
					List<Object[]> list = serviceDao.findParams(paymentAmountSql, DateUtil.getCurrYearFirst(), DateUtil.getCurrYearLast(), billCode);
					if(list!=null&&list.size()>0){
						Object[] o= list.get(0);
						BigDecimal loan_amount = (BigDecimal) o[0];
						serviceDao.excuteSql("update asset_structure_principal_daily  set  year_dc_receivable_eub =?  where bill_code =? ",loan_amount, billCode);
						count++;
					}
				} catch (Exception e) {
					logger.error("单据:[{}]更新资产结构日报表，已到期未结清余额本年代偿回款金额异常[{}]", objs[0].toString(), e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("更新更新资产结构日报表，已到期未结清余额本年代偿回款金额条数:[{}]", count);
	}

	// 根据bill_code查询【已到期未结清余额本年代偿金额】
	public void getYearDCAmountEub() {
			logger.info("开始更新资产结构日报表，已到期未结清余额本年代偿金额");
			int count = 0;
			int index = 0;
			String sql = "SELECT * FROM asset_structure_principal_daily ";
			while (index >= 0) {
				SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
				PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
				if (pageResult.getReturnDataSize() == 0)
					return;
				for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
					Object[] objs = pageResult.getData().get(i);
					try {
						String billCode = objs[1] == null ? null : (String) objs[1];
						String currentCompensatorySql = " select sum(pay_amount) as amount from o2o_ea_loan_data where is_del='N' and  pay_date BETWEEN ? AND ?   AND    bill_code = ? group by  bill_code ";
						CompensatoryEntity currCompensatoryEntity=serviceDao.findEntity(currentCompensatorySql, CompensatoryEntity.class,DateUtil.getCurrYearFirst(), DateUtil.getCurrYearLast(), billCode);
						if(currCompensatoryEntity!=null && currCompensatoryEntity.getAmount()!=null){
							serviceDao.excuteSql("update asset_structure_principal_daily  set  year_dc_amount_eub=?  where bill_code =? ",currCompensatoryEntity.getAmount(), billCode);
							count++;
						}
					} catch (Exception e) {
						logger.error("单据:[{}]更新资产结构日报表，已到期未结清余额本年代偿金额异常[{}]", objs[0].toString(), e.getMessage());
					}
				}
				if (pageResult.getPageTotal() > index + 1) {
					index++;
				} else {
					index = -1;
				}
			}
			logger.info("更新更新资产结构日报表，已到期未结清余额本年代偿金额条数:[{}]", count);
		}
		
	// 根据bill_code查询【已到期未结清余额历年累计代偿金额】
	public void getHistoryDCAmountEub() {
		logger.info("开始更新资产结构日报表，已到期未结清余额历年累计代偿金额");
		int count = 0;
		int index = 0;
		String sql = "SELECT * FROM asset_structure_principal_daily ";
		while (index >= 0) {
			SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Constant.PROCESS_DATA_LIMIT, index, sql);
			PageResult<Object[]> pageResult = serviceDao.findDataWithPageForService(sqlPageQuery);
			if (pageResult.getReturnDataSize() == 0)
				return;
			for (int i = 0; i < pageResult.getReturnDataSize(); i++) {
				Object[] objs = pageResult.getData().get(i);
				try {
					String billCode = objs[1] == null ? null : (String) objs[1];
					String currentCompensatorySql = " select sum(pay_amount) as amount from o2o_ea_loan_data where is_del='N' and  bill_code = ? group by bill_code";
					CompensatoryEntity currCompensatoryEntity=serviceDao.findEntity(currentCompensatorySql, CompensatoryEntity.class, billCode);
					if(currCompensatoryEntity!=null && currCompensatoryEntity.getAmount()!=null){
						serviceDao.excuteSql("update asset_structure_principal_daily  set  history_dc_amount_eub=?  where bill_code =? ",currCompensatoryEntity.getAmount(), billCode);
						count++;
					}
				} catch (Exception e) {
					logger.error("单据:[{}]更新资产结构日报表，已到期未结清余额历年累计代偿金额异常[{}]", objs[0].toString(), e.getMessage());
				}
			}
			if (pageResult.getPageTotal() > index + 1) {
				index++;
			} else {
				index = -1;
			}
		}
		logger.info("更新更新资产结构日报表，已到期未结清余额历年累计代偿金额条数:[{}]", count);
	}
}
